package com.esd.crm.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.esd.crm.Constants;
import com.esd.crm.entity.Doc;
import com.esd.crm.service.DocService;
import com.esd.crm.service.FilegagsService;
import com.esd.crm.util.KitService;
import com.esd.vo.PageUtil;
import com.esd.vo.ReturnResult;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/doc") 
public class DocController {

	private Logger logger = Logger.getLogger(DocController.class);
	
	@Autowired
	private DocService docService;
	@Autowired
	private FilegagsService filegagsService;
	
	/*
	 * 文件管理的查询
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public ModelAndView Doclist(){
		return new ModelAndView("doc/doc-list");
	}
	
	/*
	 * 文件查询
	 */
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public ReturnResult getDocFindAll(Doc param,PageUtil page) {
		PageInfo<Doc> pageInfo = docService.findDocByPage(param, page);
		return ReturnResult.storeData(pageInfo);
	}
	
	
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add_get(){
		ReturnResult entity = ReturnResult.success();
		entity.put(Constants.HTML_PAGE_TYPE, Constants.HTML_PAGE_TYPE_NEW);
		return new ModelAndView("doc/doc-html",entity);
		//TODO  entity是干什么的？
	}
	
	
	@ResponseBody
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public Map<String,Object> add_post(Doc doc,HttpServletRequest request,HttpSession session){
		//1. 校验用户名称是否已经存在
		Doc checkDocname=docService.findOneByDocName(doc.getDocName());
		if(checkDocname != null){
			return ReturnResult.fail("用户名称已存在.");
		}
		//2. 新增
		String docId=KitService.getUUID();;
		doc.setId(docId);
		
		System.out.println("插入一条数据的id："+docId);
		doc.toString();
		
		boolean a=docService.insertSelective(doc);
		if(!a){
			return ReturnResult.fail();
		}
		return ReturnResult.success();
	}
	
	
	@RequestMapping(value = "/edit/{id}/{editable}", method = RequestMethod.GET)
	public ModelAndView edit_get(@PathVariable(value = "id") String id, @PathVariable(value = "editable") int editable,
			HttpServletRequest request) {
		ReturnResult entity = ReturnResult.success();
		System.out.println("editid=====>"+id);
		Doc doc = docService.findOneByDocId(id);
		entity.put("doc", doc);
		System.out.println("edit修改entity:"+entity);//打桩
		entity.put(Constants.HTML_PAGE_TYPE, Constants.HTML_PAGE_TYPE_EDIT);
		return new ModelAndView("doc/doc-html", entity);
	}
	
	@ResponseBody
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public Map<String,Object> updateByPrimaryKeySelective(Doc doc,HttpServletRequest request,HttpSession session){
		String id=doc.getId();
		System.out.println("要修改信息的id："+id);
		boolean b = docService.updateByPrimaryKeySelective(doc); 
		if(!b) {
			return ReturnResult.fail();
		}
		return ReturnResult.success();
	}
	
	@RequestMapping(value="/deleteBatch",method=RequestMethod.POST)
	public Map<String,Object> deleteBatch(@RequestParam("idArray")String [] idArray,HttpServletRequest reuqest,HttpSession session){
		if(idArray == null || idArray.length ==0) {
			return ReturnResult.fail("请选择要删除的项.");
		}
		int idArrayLength = idArray.length;
		
		boolean b = docService.deleteBatch(idArray);
		if(!b) {
			return ReturnResult.fail();
		}
		for (int i = 0; i < idArrayLength; i++) {
			System.out.println("删除项的idArray[i]===>"+idArray[i]);
			filegagsService.deleteByBusinessId(idArray[i]);
		}
		return ReturnResult.success();
	}
		
		
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public Map<String,Object> deleteByPrimaryId(String id,HttpServletRequest reuqest,HttpSession session){
		boolean a = docService.deleteByPrimaryKey(id);
		if(!a){
			return ReturnResult.fail();
		}
		return ReturnResult.success();
	}
	
	
	
	
	
	
	
	
	
	
	
		
	}
	
	
	
	
	
	
	
	
	

